/*
 * Copyright (c) 2019. seven studio. @chengdu
 * seven studio in chengdu by lb
 * this project for snmp tool
 */

package com.xlx.openLink.threadtask.workthread;

import com.xlx.openLink.base.BaseUtils;
import com.xlx.openLink.base.SnmpException;
import com.xlx.openLink.entity.baseenity.MibSystem;
import com.xlx.openLink.entity.pojo.Alarm;
import com.xlx.openLink.entity.pojo.Device;
import com.xlx.openLink.enums.AlLevel;
import com.xlx.openLink.enums.AlSendTypeEnum;
import com.xlx.openLink.enums.DevStatusEnum;
import com.xlx.openLink.enums.DevTypeEnum;
import com.xlx.openLink.service.DeviceService;
import com.xlx.openLink.uitls.Commuitls;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;
import java.util.concurrent.BlockingDeque;

/**
 * @author Admin
 */
@Slf4j
public class SearchDeviceThread  {

    private BlockingDeque<Device> blockingDeque;
    private BlockingDeque<Alarm> alarmBlockingDeque;
    private List<Device> deviceList;
    private DeviceService deviceService;

    public SearchDeviceThread(BlockingDeque<Device> blockingDeque, BlockingDeque<Alarm> alarmBlockingDeque, List<Device> deviceList, DeviceService deviceService) {
        this.blockingDeque = blockingDeque;
        this.deviceList = deviceList;
        this.deviceService = deviceService;
        this.alarmBlockingDeque = alarmBlockingDeque;
    }


    public void dobiz(){
        deviceList.forEach(dev -> {
        log.info("开始定时查看设备状态---------------------");
        try {
            if (!Commuitls.ping(dev.getDevIp())) {
                log.info("设备是不线状态-------------------");
                if (dev.getDevStatus().equals(DevStatusEnum.ONLINE.getCode())) {
                    alarmBlockingDeque.add(createAlarm("设备名称: " + dev.getDevName() + "\n" + "设备IP:" + dev.getDevIp() + "  掉线", dev));
                }
                dev.setDevStatus(DevStatusEnum.OFFLINE.getCode());//设备不在线
            } else {
                if (dev.getDevStatus().equals(DevStatusEnum.OFFLINE.getCode())) {
                    alarmBlockingDeque.add(createAlarm("设备名称: " + dev.getDevName() + "\n" + "设备IP:" + dev.getDevIp() + "  恢复", dev));
                }
                log.info("设备是在线状态-------------------");
                dev.setDevStatus(DevStatusEnum.ONLINE.getCode()); //设备在线
                //如果是交换机
                if (dev.getDevType().equalsIgnoreCase(DevTypeEnum.SwitchType.getCode())) {
                    MibSystem mibSystem = deviceService.getSysInfo(dev); //获取设备信息
                    if (mibSystem != null) {
                        dev.setDevDescor(mibSystem.getSysDescr());
                        dev.setDevOid(mibSystem.getMappingOID());
                        dev.setDevSysUpTime(String.valueOf(mibSystem.getSysUpTime()));
                        dev.setDevSysName(mibSystem.getSysName());//获取系统名称
                        deviceService.getMainMAC(dev);
                    }
                }
            }
            dev.setDevUpdateDate(new Date());
            blockingDeque.add(dev);
        } catch (SnmpException e) {
            log.error("错误--------------");
            log.error(e.toString());
        } catch (Exception e) {
            log.error("错误--------------");
            log.error(e.toString());
        }
    });}


    private Alarm createAlarm(String msg, Device device) {
        Alarm alarm = new Alarm();
        alarm.setAlMsg(msg);
        alarm.setAlSendStatus(AlSendTypeEnum.UNSENT.getCode());
        alarm.setAlType(device.getDevType());
        alarm.setAlLevel(AlLevel.AL_DEADLY.getCode());
        alarm.setAlDevIp(device.getDevIp());
        alarm.setAlType(device.getDevType());
        alarm.setAlDevName(device.getDevName());
        alarm.setAlCreateDate(new Date());
        alarm.setPkGuid(BaseUtils.createGUID());
        return alarm;
    }
}


